import InvokeUtil from '@/framework/infra/util/InvokeUtil'
import BaseCURDV2Api from '@/framework/infra/base/BaseCURDV2Api'

class AdminApi extends BaseCURDV2Api {
  constructor() {
    super('mgr/admin')
  }

  resetPassword(id) {
    return this.post('/reset-password', {
      id
    })
  }

  changeLoginPwd(data) {
    return this.post('/change-pwd', data)
  }

  listAll() {
    return InvokeUtil.once(this, () => this.get('/list-all'))
  }

  listAllOfType(type) {
    return this.listAll().then(data => {
      return data.filter(el => el.adminTypeList.includes(type))
    })
  }

  list(data) {
    return super.list(data).then(data => {
      (data.data || data.records) && (data.data || data.records).forEach(item => {
        if (item.roleList) {
          item.roleIdList = item.roleList.map(el => el.id)
          item.roleNameList = item.roleList.map(el => el.title).join(',')
        } else {
          item.roleIdList = []
          item.roleNameList = ''
        }
        if (item.adminType) {
          item.adminTypeList = item.adminType.split(',')
        } else {
          item.adminTypeList = []
        }
      })
      return data
    })
  }
}

export const adminApi = new AdminApi()
